package com.leyou.auth.config;


import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.net.SocketTimeoutException;

/**
 * @Author: wangying
 * @Description:
 * @Date: Created in 10:24 2019/1/7
 */
@Aspect
@Component
public class FeignAspect {

    private static final Logger LOG = LoggerFactory.getLogger(FeignAspect.class);


    @Pointcut("execution(* com.leyou.auth.client..*.*(..))")
    public void pointCut(){}

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        String name = StringUtils.join(pjp.getSignature().getDeclaringTypeName(),".", pjp.getSignature().getName());
        String req = JSON.toJSONString(pjp.getArgs());

        try {
            LOG.info("-----【{}】---- 进入Feign调用... request: 【{}】", name, req);
            Object proceed = pjp.proceed();
            String resp = JSON.toJSONString(proceed);
            LOG.info("-----【{}】---- 退出Feign调用... request: 【{}】, response: 【{}】", name, req, resp);
            return proceed;
        } catch (SocketTimeoutException e){
            LOG.info("-----【{}】---- 异常Feign调用... request: 【{}】, e: 【{}】", name, req, JSON.toJSON(e));
        }catch (Exception e){
            LOG.info("-----【{}】---- 异常Feign调用... request: 【{}】, e: 【{}】", name, req, JSON.toJSON(e));
        }
        return pjp.proceed();
    }

}
